#include "pix5core.h"

#include <errno.h>

Condition::Condition()
{
	pthread_cond_init(&_cond,NULL);
}

Condition::~Condition()
{
	pthread_cond_destroy(&_cond);
}


bool Condition::wait(int timeout)
{
	if(timeout==-1)
	{
		pthread_cond_wait(&_cond,&_mtx);

		return true;
	}
	else
	{
		timespec ts;

		ts.tv_sec = timeout/1000;
		ts.tv_nsec = (timeout%1000)*1000000;

		if(pthread_cond_timedwait(&_cond,&_mtx,&ts)==ETIMEDOUT)
			return false;
		
		return true;
	}
}

void Condition::signal()
{
	pthread_cond_signal(&_cond);
}

void Condition::broadcast()
{
	pthread_cond_broadcast(&_cond);
}


